﻿/*
 *  This file is part of Worker Time.
 *
 *  Worker Time is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Worker Time is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Worker Time.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using WorkerTime.Database;
using System.Globalization;
using System.Data.Common;
namespace WorkerTime.Database
{
    public class Track
    {
        public Int32 ID;
        public Int32 projectId;
        public Int32 activityId;
        public DateTime startTime;
        public DateTime endTime;

        public Track()
        {
        }

        public Track(Int32 projectId, Int32 activityId)
        {
            this.projectId = projectId;
            this.activityId = activityId;            
        }
    
        public Int32 ProjectId
        {
            get
            {
                return projectId;
            }
            set
            {
                projectId = value;
            }
        }

        public Int32 ActivityId
        {
            get
            {
                return activityId;
            }
            set
            {
                activityId = value;
            }
        }

        public DateTime StartTime
        {
            get
            {
                return startTime;
            }
            set
            {
                startTime = value;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                endTime = value;
            }
        }

        public void Start()
        {
            this.StartTime = DateTime.Now;
        }

        public void Stop()
        {
            this.EndTime = DateTime.Now;
        }

        public void Save(Manager database)
        {
            if (ID == 0)
                database.ExecuteNonQuery("INSERT INTO Tracks (ProjectID, ActivityID, Start, End, Duration) VALUES('" + projectId.ToString() + "','" + activityId.ToString() + "','" + startTime.ToString("s", DateTimeFormatInfo.InvariantInfo) + "','" + endTime.ToString("s", DateTimeFormatInfo.InvariantInfo) + "','" + ElapsedTime().Ticks + "')");
            else
                database.ExecuteNonQuery("UPDATE Tracks SET ProjectID='" + projectId.ToString() + "', ActivityID='" + activityId.ToString() + "', Start='" + startTime.ToString("s", DateTimeFormatInfo.InvariantInfo) + "', End='" + endTime.ToString("s", DateTimeFormatInfo.InvariantInfo) + "', Duration='" + ElapsedTime().Ticks + "' WHERE ID='" + ID + "'");
        }

        public void Delete(Manager database)
        {
            database.ExecuteNonQuery("DELETE FROM Tracks WHERE ID = '" + this.ID + "'");
            ID = new Int32();
            projectId = new Int32();
            activityId = new Int32();
            startTime = new DateTime();
            endTime = new DateTime();
        }

        public TimeSpan ElapsedTime()
        {
            TimeSpan duration;
            if (endTime == DateTime.MinValue )
                duration = DateTime.Now - startTime;
            else
                duration = endTime - startTime;
            return duration;
        }
    }
}
